package com.limao.DBProvider;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.limao.DBDataClass.CrowdPrestigeGoodsDataClass;
import com.limao.DbConnection.DbConnectionManager;
import com.limao.Tool.CacheClass;

public class CrowdPrestigeGoodsProvider {
	
	private static class SysGoodsProviderContainer {
		private static CrowdPrestigeGoodsProvider instance = new CrowdPrestigeGoodsProvider();
	}

	public static CrowdPrestigeGoodsProvider getInstance() {
		return SysGoodsProviderContainer.instance;
	}
	



	// 初始化
	private CrowdPrestigeGoodsProvider() {

	}

	private CrowdPrestigeGoodsDataClass encapOrderData(ResultSet rs) throws Exception{
		
		CrowdPrestigeGoodsDataClass orderData = new CrowdPrestigeGoodsDataClass();

		orderData.prestigeid=rs.getString("prestigeid");
		orderData.prestigename=rs.getString("prestigename");
		orderData.prestigeimage=rs.getString("prestigeimage");
		orderData.prestigeintro=rs.getString("prestigeintro");
		orderData.introimage=rs.getString("introimage");
		
		orderData.state=rs.getString("state");
		orderData.type=rs.getString("type");
		orderData.requirevalue=rs.getString("requirevalue");
		orderData.oldprice=rs.getString("oldprice");
		orderData.iscanback=rs.getString("iscanback");
		
		orderData.callbackprice=rs.getString("callbackprice");
		orderData.buynum=rs.getString("buynum");
		orderData.limitenum=rs.getString("limitenum");
		orderData.ctime=rs.getString("ctime");
		orderData.utime=rs.getString("utime");
		
		return orderData;
	}



	//&新鲜宝   众筹活跃商品
	public ArrayList<CrowdPrestigeGoodsDataClass> PrestigeGoods()
	{
		ArrayList<CrowdPrestigeGoodsDataClass> list = new ArrayList<CrowdPrestigeGoodsDataClass>();
		Connection con = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
			try {
				 //活跃商品
				con = DbConnectionManager.getConnection();
				pstmt = con.prepareStatement("select * from cd_sys_prestigegoods where type=0 and state=1 order by requirevalue asc");
				rs =pstmt.executeQuery();
				while (rs.next()) {
					list.add(encapOrderData(rs));
				}

			} catch (Exception e) {
				e.printStackTrace();
				list .clear();
			} finally {
				DbConnectionManager.closeConnection(rs, pstmt, con);
			}

			return list;
			
	}
	
	
	//&新鲜宝   众筹活跃商品是否领取过
	public List<CrowdPrestigeGoodsDataClass> PrestigeGoodsByuserid(int userid,List<CrowdPrestigeGoodsDataClass> list)
	{
		ArrayList<String> list2 = new ArrayList<String>();
		Connection con = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
			try {
				//是否已领取过
				con = DbConnectionManager.getConnection();
				pstmt = con.prepareStatement("select prestigeid from cd_sys_prestigeorder where userid=?");
				pstmt.setInt(1, userid);
				rs =pstmt.executeQuery();
				while (rs.next()) {
					list2.add(rs.getString("prestigeid"));
				}
				
				if(list.size()>0 && list2.size()>0)
				{
					for(String strid:list2)
					{
						int prestigeid=CacheClass.getIntValue(strid);
						for(CrowdPrestigeGoodsDataClass tmp:list)
						{
							if(prestigeid==CacheClass.getIntValue(tmp.prestigeid))
							{
								tmp.isget="1";
							}
						}

					}
				}
				
				

			} catch (Exception e) {
				e.printStackTrace();
				list .clear();
			} finally {
				DbConnectionManager.closeConnection(rs, pstmt, con);
			}

			return list;
	}

	
	//&新鲜宝   众筹虚拟币商品
	public ArrayList<CrowdPrestigeGoodsDataClass> PrestigeVrGoods()
	{
		ArrayList<CrowdPrestigeGoodsDataClass> list = new ArrayList<CrowdPrestigeGoodsDataClass>();
		Connection con = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
			try {
				 //虚拟币商品
				con = DbConnectionManager.getConnection();
				pstmt = con.prepareStatement("select * from cd_sys_prestigegoods where type=1 and state=1 order by sortvalue asc");
				rs =pstmt.executeQuery();
				while (rs.next()) {
					list.add(encapOrderData(rs));
				}

			} catch (Exception e) {
				e.printStackTrace();
				list .clear();
			} finally {
				DbConnectionManager.closeConnection(rs, pstmt, con);
			}

			return list;
			
	}
	
	
	public CrowdPrestigeGoodsDataClass GetRedBagGoods(int prestigeid )
	{
		CrowdPrestigeGoodsDataClass tmp = null;
		Connection con = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
			try {
				 //活跃商品
				con = DbConnectionManager.getConnection();
				pstmt = con.prepareStatement("select * from cd_sys_prestigegoods where prestigeid=? ;");
				pstmt.setInt(1, prestigeid);
				rs =pstmt.executeQuery();
				if (rs.next()) {
					tmp=encapOrderData(rs);
				}

			} catch (Exception e) {
				e.printStackTrace();
				tmp=null;
			} finally {
				DbConnectionManager.closeConnection(rs, pstmt, con);
			}

			return tmp;
			
	}
	
	

}
